package util;

import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class Lang {
	/**
	 * Convert List to array (genericity)
	 * **/
	@SuppressWarnings("unchecked")
	public static <T> T[] conver2Array(List<T> list) {
		if (list == null || list.size() < 1)
			return null;

		Class<T> type = (Class<T>) list.get(0).getClass();
		T[] array = (T[]) Array.newInstance(type, list.size());
		list.get(0).getClass();
		for (int i = 0; i < list.size(); i++)
			array[i] = list.get(i);
		return array;
	}

	public static double[] convertDouble2double(Double[] source) {
		if (source == null)
			return null;
		double[] value = new double[source.length];
		for (int i = 0; i < source.length; i++)
			value[i] = source[i];
		return value;
	}

	public static Double[] convertdouble2Double(double[] source) {
		if (source == null)
			return null;
		Double[] value = new Double[source.length];
		for (int i = 0; i < source.length; i++)
			value[i] = source[i];
		return value;
	}

	/**
	 * Clone array from another source Array (double)
	 * **/
	public static double[] clone(double[] source) {
		if (source == null)
			return null;
		double[] clone = new double[source.length];

		for (int i = 0; i < source.length; i++)
			clone[i] = source[i];

		return clone;
	}

	/**
	 * Clone array from another source Array (int)
	 * **/
	public static int[] clone(int[] source) {
		if (source == null)
			return null;
		int[] clone = new int[source.length];

		for (int i = 0; i < source.length; i++)
			clone[i] = source[i];

		return clone;
	}

	public static int[][] clone(int[][] source) {
		if (source == null)
			return null;
		int n = source.length;
		int m = source[0].length;
		int[][] clone = new int[n][m];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++)
				clone[i][j] = source[i][j];
		}

		return clone;
	}

	/**
	 * Clone array from another source Array (genericity)
	 * **/
	@SuppressWarnings("unchecked")
	public static <T> T[] clone(T[] source) {
		if (source == null)
			return null;
		Class<T> type = (Class<T>) source[0].getClass();
		T[] clone = (T[]) Array.newInstance(type, source.length);
		for (int i = 0; i < source.length; i++)
			clone[i] = source[i];

		return clone;
	}

	/**
	 * Clone array from another source Array (genericity)
	 * **/
	@SuppressWarnings("unchecked")
	public static <T> T[][] clone(T[][] source) {
		if (source == null)
			return null;
		Class<T> type = (Class<T>) source[0].getClass();
		T[][] clone = (T[][]) Array.newInstance(type, source.length);
		for (int i = 0; i < source.length; i++) {
			clone[i] = clone(source[i]);
		}

		return clone;
	}

	/**
	 * Clone array from another source List (genericity)
	 * **/
	public static <T> List<T> clone(List<T> source) {
		if (source == null || source.size() == 0)
			return null;

		List<T> clone = new ArrayList<>();
		for (T t : source)
			clone.add(t);

		return clone;
	}

	/**
	 * Print the array on the console (genericity)
	 * **/
	public static <T> void print(T[] array) {
		if (array == null)
			return;
		for (int i = 0; i < array.length; i++)
			System.out.print(array[i] + "\t");
		System.out.println();
	}

	/**
	 * Get current direction of the project
	 * **/
	public static String projectRoot() {
		return System.getProperty("user.dir");
	}

	public static void print(int[] array) {
		if (array == null)
			return;
		for (int i = 0; i < array.length; i++)
			System.out.print(array[i] + "\t");
		System.out.println();
	}

	public static void print(double[] array) {
		if (array == null)
			return;
		for (int i = 0; i < array.length; i++)
			System.out.print(array[i] + "\t");
		System.out.println();
	}

	public static void print(double[] array, DecimalFormat format) {
		for (int i = 0; i < array.length; i++)
			System.out.print(format.format(array[i]) + "\t");
		System.out.println();

	}

	public static SimpleDateFormat DATE_FORMAT = new SimpleDateFormat(
			"yyyyMdHHmm");
}
